import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Index',
    component: () => import('./views/index.vue'),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('./views/login.vue'),
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach((to, _from, next) => {
  if (to.name === 'Login' || to.name === 'Init') {
    next();
    return;
  }
  if (localStorage.getItem('token')) {
    next();
  } else {
    next({ name: 'Login' });
  }
});

export default router;